package com.module.prms.manager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;

import base.dao.GenericHibernateDao;
import base.manager.GenericHibernateManager;
import base.util.Page;
import base.util.StringUtil;

import com.module.prms.ReqBaseBean;
import com.module.prms.dao.MenuDao;
import com.module.prms.dao.OperationDao;
import com.module.prms.dao.RoleDao;
import com.module.prms.dao.UserDao;
import com.module.prms.dao.UserOrgDao;
import com.module.prms.vo.PrmsMenu;
import com.module.prms.vo.PrmsOperation;

/**
 * 
 * <权限-操作服务类>
 * 
 * @author huhui
 * @version [1.0, July 18, 2011]
 * @see [相关类/方法]
 * @since [二代产品/权限组件]
 */
public class OperationManager extends GenericHibernateManager<PrmsOperation, String> {

	static Logger logger = Logger.getLogger(OperationManager.class.getName());
	OperationDao dao = OperationDao.getInstance();

	MenuDao mdao = MenuDao.getInstance();

	RoleDao rdao = RoleDao.getInstance();

	UserDao udao = UserDao.getInstance();

	UserOrgDao uodao = UserOrgDao.getInstance();

	private OperationManager() {
	}

	private static OperationManager manager;

	public synchronized static OperationManager getInstance() {
		if (manager == null)
			manager = new OperationManager();
		return manager;
	}

	/**
	 * <保存> <将菜单信息保存到数据库>
	 * 
	 * @param bean
	 * @see [类、类#方法、类#成员]
	 */
	public boolean add(PrmsOperation obj) throws Exception {
		boolean isTrue = this.saveOrUpdate(obj);
		return isTrue;
	}

	/**
	 * <删除> <将操作信息从数据库删除>
	 * 
	 * @param bean
	 * @see [类、类#方法、类#成员]
	 */
	public void del(PrmsOperation obj) throws Exception {
		this.delete(obj);
	}

	/**
	 * <删除> <将操作信息从数据库删除>
	 * 
	 * @param bean
	 * @see [类、类#方法、类#成员]
	 */
	public boolean remove(List<String> list) throws Exception {
		boolean isTrue = false;
		Session session = this.getSession();
		Transaction tx = null;
		tx = session.beginTransaction();
		try {
			for (String s : list) {
				isTrue = dao.deleteByKey(session, s);
			}
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw e;
		} finally {
			this.closeSession();
		}
		return isTrue;
	}

	/**
	 * <修改> <修改数据库中的菜单信息>
	 * 
	 * @param bean
	 * @see [类、类#方法、类#成员]
	 */
	public void upd(PrmsOperation obj) throws Exception {
		this.update(obj);
	}

	/**
	 * <根据id查找数据> <列表显示数据库用户信息>
	 * 
	 * @param bean
	 * @see [类、类#方法、类#成员]
	 */
	public PrmsOperation findebyid(PrmsOperation obj) throws Exception {
		PrmsOperation pot = new PrmsOperation();
		PrmsMenu pm = new PrmsMenu();
		Session session = this.getSession();
		try {
			pot = dao.get(session, obj.getCoperationId());
			pm = mdao.get(session, pot.getCmenuId());
		} catch (Exception e) {
			throw e;
		} finally {
			this.closeSession();
		}
		pot.setCmenuname(pm.getCmenuName());
		return pot;
	}

	/**
	 * 分页查询
	 * 
	 * @param bean
	 */
	public Page list(Page pageobj,PrmsOperation prmsOperation) throws Exception {
		Session session = this.getSession();
		base.util.Page page = null;
		try {
			page = dao.getList(session, prmsOperation, pageobj.getStart(), pageobj.getLimit());
		} catch (Exception e) {
			throw e;
		} finally {
			this.closeSession();
		}
		return page;
	}

	/**
	 * 验证企业下是否存在用户角色或者用户
	 * 
	 * @param bean
	 * @throws Exception
	 * @author dengsq
	 */
	public void checkUserOrRoleByOrg(ReqBaseBean bean) throws Exception {
//		String orgId = bean.getPrmsrole().getOrgid();
//		Session session = this.getSession();
//		boolean flag = false;
//		try {
//			long roleCout = rdao.getRoleByOrg(session, orgId);
//			List<String> useridList = uodao.getOrgUser(session, orgId);
//			long userCout = udao.getCountByOrg(session,
//					StringUtil.getNewStrByList(useridList));
//			if (roleCout > 0 || userCout > 0) {
//				flag = true;
//			}
//		} catch (Exception e) {
//			throw e;
//		} finally {
//			this.closeSession();
//		}
//		bean.setResult(RequestManager.getCheckInfo(flag));
	}

	@Override
	public GenericHibernateDao<PrmsOperation, String> getDao() {
		return dao;
	}

}
